package com.clouddisk.web.controller.system;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clouddisk.common.annotation.Log;
import com.clouddisk.common.core.controller.BaseController;
import com.clouddisk.common.core.domain.R;
import com.clouddisk.common.core.domain.dto.CommonFileDTO;
import com.clouddisk.common.core.domain.entity.CommonFile;
import com.clouddisk.common.core.domain.entity.FilePermission;
import com.clouddisk.common.core.domain.entity.UserFile;
import com.clouddisk.common.enums.BusinessType;
import com.clouddisk.common.utils.FilesUtils;
import com.clouddisk.system.domain.vo.CommonFileListVo;
import com.clouddisk.system.domain.vo.CommonFileUser;
import com.clouddisk.system.domain.vo.file.FileListVo;
import com.clouddisk.system.service.ICommonFileService;
import com.clouddisk.system.service.IFilePermissionService;
import com.clouddisk.system.service.IUserFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件共享接口
 * @author ZJH
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/common")
public class SysCommonFileController extends BaseController {

    @Resource
    ICommonFileService commonFileService;
    @Resource
    IFilePermissionService filePermissionService;
    @Resource
    IUserFileService userFileService;

    @Log(title = "文件共享", businessType = BusinessType.INSERT)
    @PostMapping(value = "/commonfile")
    @ResponseBody
    public R<String> commonFile(@RequestBody CommonFileDTO commonFileDTO) {
        CommonFile commonFile = new CommonFile();
        commonFile.setUserFileId(commonFileDTO.getUserFileId());
        commonFile.setFileDeptName(commonFileDTO.getFileDeptName());
        commonFile.setCommonFileId(IdUtil.getSnowflakeNextIdStr());

        commonFileService.save(commonFile);

        List<FilePermission> list = JSON.parseArray(commonFileDTO.getCommonUserList(), FilePermission.class);

        List<FilePermission> filePermissionList = new ArrayList<>();
        for (FilePermission filePermission : list) {
            filePermission.setFilePermissionId(IdUtil.getSnowflakeNextId());
            filePermission.setCommonFileId(commonFile.commonFileId);
            filePermission.setFileDeptName(commonFile.fileDeptName);
            filePermissionList.add(filePermission);
        }
        filePermissionService.saveBatch(filePermissionList);

        return R.ok();
    }

    @GetMapping(value = "/commonfileuser")
    @ResponseBody
    public R<List<CommonFileUser>> commonFileUserList() {
        List<CommonFileUser> list = commonFileService.selectCommonFileUser(getUserId());
        return R.ok(list);
    }

    @RequestMapping(value = "/getCommonFileByUser", method = RequestMethod.GET)
    @ResponseBody
    public R<List<CommonFileListVo>> getCommonFileByUser(
         Long userId){
        List<CommonFileListVo> commonFileVo = commonFileService.selectCommonFileByUser(userId, getUserId());
        return R.ok(commonFileVo);
    }

    @Log(title = "获取共享空间中某个用户的文件列表", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/commonFileList", method = RequestMethod.GET)
    @ResponseBody
    public R<IPage<FileListVo>> commonFileList(Long commonFileId, String filePath, long currentPage, long pageCount){

        CommonFile commonFile = commonFileService.getById(commonFileId);
        UserFile userFile = userFileService.getById(commonFile.getUserFileId());
        FilesUtils filesUtils = new FilesUtils(userFile.getFilePath(), filePath, true);
        IPage<FileListVo> fileList = userFileService.userFileList(userFile.getUserId(), userFile.getFileDeptName(), filesUtils.getPath(), currentPage, pageCount);

        return R.ok(fileList);

    }
}
